﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Channels;
using MySql;

namespace KoPortal.Sys.Data.MySQL
{
    public static class DBKoLocal
    {
        #region Members
        private static string _connStr = "";
        public static string ConnStr
        {
            get
            {
                _connStr = GetConnStrLocal();
                return _connStr;
            }
        }
        #endregion
        #region Common Methods
        public static string GetConnStrLocal()
        {
            //KoEnvironment.GetAppSetting("MySqlMainSrvConnect");
            string ret = KoEnvironment.DBMySQLMainConnStr;
            return ret;
        }
        #endregion Common Methods
        #region Get Data
        public static Exception GetMySqlSrvList(ref DataTable parTbl)
        {
            Exception ret = null;
            string sql =
@"SELECT * FROM shops_sale WHERE (accept=1) ORDER BY name";
            try
            {
                using (MySql.Data.MySqlClient.MySqlConnection conn =
                    new MySql.Data.MySqlClient.MySqlConnection(ConnStr))
                {
                    conn.Open();
                    using (MySql.Data.MySqlClient.MySqlCommand cmd =
                        new MySql.Data.MySqlClient.MySqlCommand(sql, conn))
                    {
                        cmd.CommandTimeout = 8000;
                        cmd.CommandType = global::System.Data.CommandType.Text;
                        using (MySql.Data.MySqlClient.MySqlDataAdapter a =
                            new MySql.Data.MySqlClient.MySqlDataAdapter(cmd))
                        {
                            parTbl = new DataTable();
                            a.Fill(parTbl);
                        }
                    }
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
        public static Exception GetMySqlSrvList_In88(ref DataTable parTbl)
        {
            Exception ret = null;
            try
            {
                string ProxyDomainName = KoEnvironment.GetAppSetting("ProxyDomainName");
                string ProxyUID = KoEnvironment.GetAppSetting("ProxyUID");
                string ProxyPwd = KoEnvironment.GetAppSetting("ProxyPwd");

                string ServiceUID = KoEnvironment.GetAppSetting("WebServiceUser");
                string ServicePWD = KoEnvironment.GetAppSetting("WebServicePass");

                ret = GetMySqlSrvList_In88(
                    ref parTbl, ProxyDomainName, ProxyUID, ProxyPwd, ServiceUID, ServicePWD);
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
        public static Exception GetMySqlSrvList_In88(ref DataTable parTbl, string ServiceUID, string ServicePWD)
        {
            Exception ret = null;
            try
            {
                string ProxyDomainName = KoEnvironment.GetAppSetting("ProxyDomainName");
                string ProxyUID = KoEnvironment.GetAppSetting("ProxyUID");
                string ProxyPwd = KoEnvironment.GetAppSetting("ProxyPwd");

                ret = GetMySqlSrvList_In88(
                    ref parTbl, ProxyDomainName, ProxyUID, ProxyPwd, ServiceUID, ServicePWD);
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
        public static Exception GetMySqlSrvList_In88(ref DataTable parTbl, string parProxyDomainName, string parProxyUID, string parProxyPwd, string parServiceUID, string parServicePWD)
        {
            Exception ret = null;
            try
            {
                BasicHttpBinding binding = AServiceConfiguration.Binding;

                binding.Security.Mode =
                    BasicHttpSecurityMode.TransportCredentialOnly;
                binding.UseDefaultWebProxy = false;
                //binding.ProxyAddress =
                //   new Uri(KoEnvironment.GetAppSetting("ProxyAddress"));
                //binding.BypassProxyOnLocal = false;
                binding.Security.Transport.ProxyCredentialType =
                    HttpProxyCredentialType.Windows;
                binding.Security.Transport.ClientCredentialType =
                    HttpClientCredentialType.Basic;

                var client = new KoReference88Common.WebServiceCommonSoapClient(
                    binding, AServiceConfiguration.Endpoint);

                client.ClientCredentials.Windows.ClientCredential.Domain = parProxyDomainName;
                client.ClientCredentials.Windows.ClientCredential.UserName = parProxyUID;
                client.ClientCredentials.Windows.ClientCredential.Password = parProxyPwd;

                client.ClientCredentials.UserName.UserName = parServiceUID;
                client.ClientCredentials.UserName.Password = parServicePWD;
                
                parTbl = client.GetMySqlSrvList(new DateTime(2005, 1, 1));
            }
            catch(Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
        #endregion Get Data
    }

    public class AServiceConfiguration
    {
        /// <summary>
        /// Gets a configured binding for the service.
        /// </summary>
        public static BasicHttpBinding Binding
        {
            get
            {
                BasicHttpBinding _binding = new BasicHttpBinding()
                {
                    Name = "BasicHttpBinding_IAmAService",
                    CloseTimeout = new TimeSpan(0, 1, 0),
                    OpenTimeout = new TimeSpan(0, 1, 0),
                    ReceiveTimeout = new TimeSpan(0, 10, 0),
                    SendTimeout = new TimeSpan(0, 1, 0),
                    AllowCookies = false,
                    BypassProxyOnLocal = false,
                    HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
                    MaxBufferSize = 65536,
                    MaxBufferPoolSize = 524288,
                    MaxReceivedMessageSize = 65536,
                    MessageEncoding = WSMessageEncoding.Text,
                    TextEncoding = Encoding.UTF8,
                    TransferMode = TransferMode.Buffered,
                    UseDefaultWebProxy = true,
                    ReaderQuotas = new XmlDictionaryReaderQuotas()
                    {
                        MaxDepth = 32,
                        MaxStringContentLength = 8192,
                        MaxArrayLength = 2097151,
                        MaxBytesPerRead = 4096,
                        MaxNameTableCharCount = 16384
                    }
                };

                return _binding;
            }
        }
        /// <summary>
        /// Gets the endpoint for the service.
        /// </summary>
        public static EndpointAddress Endpoint
        {
            get
            {
                return new EndpointAddress(
                    KoEnvironment.WebServiceUrl + "WebServiceCommon.asmx");
            }
        }
    }

}
